package com.example.base.security.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.base.config.PageData;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.security.IsSecurity;
import com.example.base.security.SecurityUtil;
import com.example.base.security.SysUser;
import com.example.base.security.domain.SysDept;
import com.example.base.security.domain.SysUserEntity;
import com.example.base.security.domain.SysUserRoleEntity;
import com.example.base.security.domain.vo.SysUserRegisterVo;
import com.example.base.security.domain.vo.SysUserVo;
import com.example.base.security.service.*;
import com.example.base.utils.Result;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"用户模块"})
@RestController
@RequestMapping("user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SelfUserDetailsService service;

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @ApiResponses(value = {
            @ApiResponse(code = 200,message = "注册成功",response = JSONObject.class),
            @ApiResponse(code = 1,message = "注册失败",response = JSONObject.class)
    })
    @IsSecurity(isSecurity=false)
    @PostMapping("register")
    @ApiOperation("用户注册接口")
    public JSONObject registerController(@Validated @RequestBody SysUserRegisterVo vo) throws GlobalExcetion {
        service.register(vo);
        return Result.successInfo("注册成功");
    }

    @ApiOperation("分页")
    @GetMapping("page")
    public JSONObject page(@RequestParam(value = "page",defaultValue = "1")Integer page,
                           @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                           @RequestParam(value = "name",required = false)String name,
                           @RequestParam(value = "deptId",required = false)Integer deptId){
        List<Integer> deptIds=null;
        if(deptId!=null){
            List<SysDept> list=sysDeptService.childsFilterChildById(deptId);
            if(list!=null && list.size()>0){
                deptIds=list.stream().map(SysDept::getId).collect(Collectors.toList());
            }
        }
        IPage<SysUserEntity> sysUserEntityIPage=sysUserService.page(page,pageSize,name,deptIds);
        PageData pageData=new PageData(sysUserEntityIPage.getTotal(),page,pageSize,sysUserEntityIPage.getRecords());
        return Result.successInfo(pageData);
    }

    @ApiOperation("添加/更新用户")
    @PostMapping("addorupdate")
    public JSONObject addOrUpDate(@Validated @RequestBody SysUserVo userVo) throws GlobalExcetion {
        service.addOrUpdateSysUserEntity(userVo);
        return Result.successInfo("操作成功");
    }


    @ApiOperation("获取用户角色")
    @GetMapping("getUserRole")
    public JSONObject getUserRole(@RequestParam("userId")String userId){
        if(!StringUtils.isNotBlank(userId)){
            return Result.errorInfo("用户id不得为空");
        }
        List<SysUserRoleEntity> sysUserRoleEntityList=sysUserRoleService.selectUserRoleEntityByUid(userId);
        return Result.successInfo(sysUserRoleEntityList);
    }

    @Autowired
    private PasswordService passwordService;

    @ApiOperation("密码重置")
    @PostMapping("passwordrestart")
    public JSONObject passWordRestart(@RequestBody JSONObject jsonObject){
        String text="123456";
        if(jsonObject.containsKey("ids")){
            passwordService.restartPassword((List<String>) jsonObject.get("ids"));
        }
        return Result.successInfo("密码重置成功");
    }
}
